﻿package com.yy.manager {
	import com.yy.base.App;
	import com.yy.manager.base.IFollowToolTip;
	import com.yy.manager.base.IToolTip;
	import com.yy.manager.base.ToolTipPool;
	import com.yy.ui.core.ITipComponent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;

	/**
	 *tooltips管理器 
	 * @author YY
	 * 
	 */
    public class ToolTipManager {
		//箭头方向
        public static const DOWN:String = "down";
        public static const UP:String = "up";
        public static const LEFT:String = "left";
        public static const RIGHT:String = "right";

        private var _stage:Stage;
        private var _parent:DisplayObjectContainer;
        private var _popTipsParent:DisplayObjectContainer;
        private var _toolTipList:Object;//Itooltip 类容器
        protected var _mouseOverToolTipList:Dictionary;
        private var _currentTooltip:IToolTip;

        public function ToolTipManager(){
            this._toolTipList = {};
            super();
            this._stage = App.appStage;
            this._stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMoveHandler);
            this._mouseOverToolTipList = new Dictionary();
        }
        public function get parent():DisplayObjectContainer{
            return (((this._parent) || (this._stage)));
        }
        public function set parent(_arg1:DisplayObjectContainer):void{
            this._parent = _arg1;
        }
        public function get popTipsParent():DisplayObjectContainer{
            return (this._popTipsParent);
        }
        public function set popTipsParent(_arg1:DisplayObjectContainer):void{
            this._popTipsParent = _arg1;
        }
		/**
		 *注册tooltips类 
		 * @param cls
		 * @param key
		 * 
		 */
        public function registerToolTip(cls:Class, key:String="simple"):void{
            this._toolTipList[key] = cls;
        }
        private function mouseMoveHandler(e:MouseEvent):void{
            this.mouseMove(this._currentTooltip);
        }
        public function mouseMove(tips:IToolTip):void{
            if (((!((tips == null))) && ((tips is IFollowToolTip)))){
                if ((tips as DisplayObject).visible == true){
                    (tips as IFollowToolTip).move(this._stage.mouseX, this._stage.mouseY);
                };
            };
        }
        public function mouseOver(_arg1:Object):void{
            var _local2:ITipComponent;
            if ((_arg1 is ITipComponent)){
                _local2 = (_arg1 as ITipComponent);
                if (((!((_local2.toolTip == ""))) || (!((_local2.toolTipData == null))))){
                    this.show(_local2, _local2.toolTip, _local2.toolTipDirection, _local2.toolTipData, _local2.toolTipType);
                };
            };
        }
        public function mouseOut(_arg1:Object):void{
            var _local2:ITipComponent;
            if ((_arg1 is ITipComponent)){
                _local2 = (_arg1 as ITipComponent);
                this.hide(_local2);
            };
        }
        public function show(_arg1:Object, _arg2:String="", _arg3:String="down", _arg4:Object=null, _arg5:String="simple"):void{
            var _local6:Class = this._toolTipList[_arg5];
            var _local7:IToolTip = ToolTipPool.getToolTip(_local6);
            this._currentTooltip = _local7;
            this._mouseOverToolTipList[_arg1] = _local7;
            if (((!((this.popTipsParent == null))) && (!((UiManager.getPopUpManager().getWindow() == null))))){
                this.popTipsParent.addChild((_local7 as DisplayObject));
            } else {
                this.parent.addChild((_local7 as DisplayObject));
            };
            _local7.show(_arg1, _arg2, _arg3, _arg4);
            if ((_local7 is IFollowToolTip)){
                (_local7 as IFollowToolTip).move(this._stage.mouseX, this._stage.mouseY);
            };
        }
        public function hide(key:Object):void{
            if (this._mouseOverToolTipList[key] != null){
                (this._mouseOverToolTipList[key] as IToolTip).hide();
                delete this._mouseOverToolTipList[key];
            };
        }
        public function get currentTooltip():IToolTip{
            return (this._currentTooltip);
        }

    }
}
